package com.xdtech.hotel.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xdtech.common.utils.EmptyUtil;
import com.xdtech.core.model.BaseItem;
import com.xdtech.core.orm.Page;
import com.xdtech.core.orm.utils.BeanUtils;
import com.xdtech.hotel.dao.HotelInfoDao;
import com.xdtech.hotel.model.HotelInfo;
import com.xdtech.hotel.service.HotelInfoService;
import com.xdtech.hotel.vo.HotelInfoItem;
import com.xdtech.web.model.Pagination;

/**
 * 
 * @author max.zheng
 * @create 2015-04-04 11:00:12
 * @since 1.0
 * @see
 */
@Service
public class HotelInfoServiceImpl implements HotelInfoService {
	@Autowired
	private HotelInfoDao hotelInfoDao;

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:00:12
	 * @modified by
	 * @param entity
	 */
	public void save(HotelInfo entity) {
		hotelInfoDao.save(entity);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:00:12
	 * @modified by
	 * @param entity
	 */
	public void saveAll(List<HotelInfo> entities) {
		hotelInfoDao.saveAll(entities);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:00:12
	 * @modified by
	 * @param entity
	 */
	public void delete(HotelInfo entity) {
		hotelInfoDao.delete(entity);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:00:12
	 * @modified by
	 * @param id
	 */
	public void delete(Long id) {
		hotelInfoDao.delete(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:00:12
	 * @modified by
	 * @param id
	 * @return
	 */
	public HotelInfo get(Long id) {
		return hotelInfoDao.get(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:00:12
	 * @modified by
	 * @return
	 */
	public List<HotelInfo> getAll() {
		return hotelInfoDao.getAll();
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:00:12
	 * @modified by
	 * @param pg
	 * @param values
	 * @return
	 */
	public Map<String, Object> loadPageAndCondition(Pagination pg,
			Map<String, String> values) {
		Map<String, Object> results = new HashMap<String, Object>();
		List<Object> hotelInfos = null;
		long rows = 0;
		if (pg!=null) {
			Page<HotelInfo> page = hotelInfoDao.findPage(pg,"from HotelInfo order by createTime desc", values);
			hotelInfos = BeanUtils.copyListProperties(
					HotelInfoItem.class, page.getResult());
			rows = page.getTotalItems();
		}else {
			List<HotelInfo> hotelInfoList = hotelInfoDao.find("from HotelInfo order by id desc", values);
			hotelInfos = BeanUtils.copyListProperties(
					HotelInfoItem.class, hotelInfoList);
			rows = hotelInfos.size();
		}
		results.put("total", rows);
		results.put("rows", hotelInfos);
		return results;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:00:12
	 * @modified by
	 * @param item
	 * @return
	 */
	public boolean saveOrUpdateHotelInfo(HotelInfoItem item) {
		HotelInfo hotelInfo = null;
		if (item.getId()==null) {
			hotelInfo = new HotelInfo();
		}else {
			hotelInfo = hotelInfoDao.get(item.getId());
		}
		//澶嶅埗鍓嶅彴淇敼鐨勫睘鎬�
		BeanUtils.copyProperties(hotelInfo, item);
		hotelInfoDao.save(hotelInfo);
		return true;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:00:12
	 * @modified by
	 * @param hotelInfoId
	 * @return
	 */
	public HotelInfoItem loadHotelInfoItem(Long hotelInfoId) {
		HotelInfo hotelInfo = hotelInfoDao.get(hotelInfoId);
		HotelInfoItem hotelInfoItem = new HotelInfoItem();
		BeanUtils.copyProperties(hotelInfoItem, hotelInfo);
		return hotelInfoItem;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:00:12
	 * @modified by
	 * @param id
	 * @return
	 */
	public boolean deleteHotelInfoInfo(long id) {
		delete(id);
		return true;
	}
	
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:00:12
	 * @modified by
	 * @param newIds
	 * @return
	 */
	public boolean deleteHotelInfoInfo(List<Long> hotelInfoIds) {
		for (Long id : hotelInfoIds) {
			delete(id);
		}
		return true;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-4-5上午9:28:42
	 * @modified by
	 * @return
	 */
	@Override
	public HotelInfoItem loadOneHotelInfoItem() {
		List<HotelInfo> hotelInfos = getAll();
		if (EmptyUtil.isNotEmpty(hotelInfos)) {
			return HotelInfoItem.createItem(hotelInfos.get(0));
		}else {
			return null;
		}
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-4-18下午12:17:00
	 * @modified by
	 * @param pg
	 * @param baseItem
	 * @param queryName
	 * @return
	 */
	@Override
	public Map<String, Object> loadPageDataByConditions(Pagination pg,
			BaseItem baseItem, String queryName) {
		// TODO Auto-generated method stub
		return null;
	}

}
